from django.contrib import admin
from .models import (
    Company, Project, TenderAnnouncement, BidOpeningRecord, BidOpeningDetail,
    BidEvaluationResult, BidCandidate, WinningResultAnnouncement, TransactionResult,
    TenderClarification, ProjectVersion, SubEntityVersion, VersionRelationship
)
class IdSearchAdmin(admin.ModelAdmin):
    def get_search_results(self, request, queryset, search_term):
        qs, use_distinct = super().get_search_results(request, queryset, search_term)
        term = (search_term or '').strip()
        if term.isdigit():
            try:
                qs = queryset.filter(pk=int(term)) | qs
            except Exception:
                pass
        return qs, use_distinct


@admin.register(Company)
class CompanyAdmin(IdSearchAdmin):
    list_display = ('company_name', 'unified_social_credit_code', 'created_at', 'updated_at')
    search_fields = ('id', 'company_name', 'unified_social_credit_code')
    list_filter = ('created_at', 'updated_at')
    readonly_fields = ('created_at', 'updated_at')


@admin.register(Project)
class ProjectAdmin(IdSearchAdmin):
    list_display = ('project_code', 'project_name', 'owner_name', 'budget_amount', 'created_at')
    search_fields = ('id', 'project_code', 'project_name', 'owner_name')
    list_filter = ('project_type', 'created_at', 'updated_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('crawl_task',)


@admin.register(TenderAnnouncement)
class TenderAnnouncementAdmin(IdSearchAdmin):
    list_display = ('announcement_date', 'source_platform', 'project', 'created_at')
    search_fields = ('id', 'original_url', 'source_platform')
    list_filter = ('announcement_date', 'source_platform', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('project', 'crawl_task')


@admin.register(BidOpeningRecord)
class BidOpeningRecordAdmin(IdSearchAdmin):
    list_display = ('opening_time', 'opening_location', 'project', 'created_at')
    search_fields = ('id', 'opening_location')
    list_filter = ('opening_time', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('project', 'crawl_task')


@admin.register(BidOpeningDetail)
class BidOpeningDetailAdmin(IdSearchAdmin):
    list_display = ('bid_price', 'bid_status', 'company', 'bid_opening_record', 'created_at')
    search_fields = ('id', 'company__company_name')
    list_filter = ('bid_status', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('company', 'bid_opening_record', 'crawl_task')


@admin.register(BidEvaluationResult)
class BidEvaluationResultAdmin(IdSearchAdmin):
    list_display = ('announcement_date', 'project', 'created_at')
    search_fields = ('id', 'original_url')
    list_filter = ('announcement_date', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('project', 'crawl_task')


@admin.register(BidCandidate)
class BidCandidateAdmin(IdSearchAdmin):
    list_display = ('candidate_rank', 'bid_price', 'company', 'bid_evaluation_result', 'created_at')
    search_fields = ('id', 'company__company_name')
    list_filter = ('candidate_rank', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('company', 'bid_evaluation_result', 'crawl_task')


@admin.register(WinningResultAnnouncement)
class WinningResultAnnouncementAdmin(IdSearchAdmin):
    list_display = ('announcement_date', 'display_winning_companies', 'winning_amount', 'project', 'created_at')
    search_fields = ('id', 'original_url', 'winning_company__company_name', 'winning_companies__company_name')
    list_filter = ('announcement_date', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('winning_company', 'project', 'crawl_task', 'winning_companies')
    filter_horizontal = ('winning_companies',)

    def display_winning_companies(self, obj):
        names = [c.company_name for c in obj.winning_companies.all()]
        if names:
            return '、'.join(names)
        return obj.winning_company.company_name
    display_winning_companies.short_description = '中标公司'


@admin.register(TransactionResult)
class TransactionResultAdmin(IdSearchAdmin):
    list_display = ('sign_date', 'contract_code', 'contract_amount', 'company', 'project', 'created_at')
    search_fields = ('id', 'contract_code', 'company__company_name')
    list_filter = ('sign_date', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('company', 'project', 'crawl_task')


@admin.register(TenderClarification)
class TenderClarificationAdmin(IdSearchAdmin):
    list_display = ('clarification_date', 'clarification_type', 'project', 'created_at')
    search_fields = ('id', 'clarification_type')
    list_filter = ('clarification_date', 'clarification_type', 'created_at')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('project', 'crawl_task')


@admin.register(ProjectVersion)
class ProjectVersionAdmin(IdSearchAdmin):
    list_display = ('project_code', 'version_number', 'html_timestamp', 'created_at')
    search_fields = ('id', 'project_code', 'version_number')
    list_filter = ('html_timestamp', 'created_at')
    readonly_fields = ('created_at',)


@admin.register(SubEntityVersion)
class SubEntityVersionAdmin(IdSearchAdmin):
    list_display = ('entity_type', 'entity_key', 'html_timestamp', 'is_active', 'change_type', 'created_at')
    search_fields = ('id', 'entity_key')
    list_filter = ('entity_type', 'html_timestamp', 'is_active', 'change_type', 'created_at')
    readonly_fields = ('created_at',)


@admin.register(VersionRelationship)
class VersionRelationshipAdmin(IdSearchAdmin):
    list_display = ('relationship_type', 'source_entity_type', 'target_entity_type', 'created_at')
    search_fields = ('id', 'relationship_type', 'source_entity_key', 'target_entity_key')
    list_filter = ('relationship_type', 'source_entity_type', 'target_entity_type', 'created_at')
    readonly_fields = ('created_at',)